﻿$(document).ready(function() {
	// Hover effects

	$('.hoverpop .hover').hover(function() {
		$(this).css('background-color','#303030').parent().find('.popdown').stop(true, true).fadeIn('fast');
	}, function() {
		$(this).css('background-color','transparent').parent().find('.popdown').stop(true, true).fadeOut('normal');
	});

	$('.hoverpop .popdown').hover(function() {
		$(this).stop(true, true).show().parent().find('.hover').css('background-color', '#303030');
	}, function() {
		$(this).stop(true, true).fadeOut('normal').parent().find('.hover').css('background-color', 'transparent');
	});

	$('.clickpop .click').click(function() {
		$('.clickpop .popdown').stop(true, true).slideUp('fast');

		$(this).parents('.clickpop').find('.popdown').slideDown('normal');

		return false;
	});

	// Remove the text on focus

	$('#query').focus(function() {
		if ($(this).val() == 'Leita að efni')
			$(this).val('').addClass('focus');
	});

	$('#query').blur(function() {
		if ($(this).val() == '')
			$(this).val('Leita að efni').removeClass('focus');
	});

	// Corners and borders on inputs

        $('.hoverpop').find('.hover').corner('round top 5px').parent().find('.popdown').corner('round bottom tr 5px');
	$('#info h3').corner('round 5px');

	$('#toolbar .hover:only-child').corner('round 5px');

	$('#searchbox #query').corner('round left 3px');
	$('#searchbox #submitsearch').corner('round right 3px');
	$('#searchbox').css('padding','2px').corner('round 5px');
	$('input').add('textarea').not(':hidden').not('#query').not('#submitsearch').css('margin-bottom','0').wrap('<div class="inputborder">').corner('round 2px').parent().corner('round 4px').css('display','inline-block').css('float', function() { return $(this).find('input').css('float'); }).find('input').css('float','none');
	$('input.submit').parent().css('float','right');

	$('#info #comments .popdown').slideUp('fast');

	// Odd columns of tables highlighted

	$('table').each(function () {
		$(this).find('tr:not(:has(th))').each(function (index) {
			if (index % 2 != 0 && !$(this).hasClass('odd'))
			{
				$(this).addClass('odd');
			}
		});
	});
	
	// AJAX THE AMAZING GREEK WARRIOR
	
	$('#rate .popdown a').click(function() {
		$.get($(this).attr('href'), function () {
			$('#rate .popdown').load(location.href + ' #rate .popdown a', {}, function() {
				$('#rate .popdown a:last').after('<span class="text">Einkunn gefin</span>');
			});
		});
		
		return false;
	});

	$('#comments form input.btn').click(function () {
		$.post($('#comments form').attr('action'), { comment: $('#comments form #comment').val() }, function () {
			$('#comments .popdown').load(location.href + ' #comments .popdown table');
		});
		
		return false;
	});

	var showcaseClosedSize = 105;
	var showcaseOpenSize = 381;
	var showcaseOffset = 0;
	$('#showcase').find('.show').corner('round 5px').wrap('<div class="showborder clearfix">').parent().corner('round 7px').each(function (index) {
		$(this).css('left', (index * showcaseClosedSize) - showcaseOffset + 'px').css('z-index', 10 - index);

		$(this).mouseenter(function () {
			$('#showcase .showborder:lt(' + $(this).index('#showcase .showborder') + ')').each(function (jindex) {
				$(this).animate({ left: (((jindex+1) * showcaseClosedSize) - showcaseOpenSize) + 'px' }, { queue: false });
			});

			$('#showcase .showborder:gt(' + $(this).index('#showcase .showborder') + ')').each(function () {
				$(this).animate({ left: ($(this).index('#showcase .showborder') * showcaseClosedSize) - showcaseOffset + 'px' }, { queue: false });
			});

			$(this).animate({ left: ($(this).index('#showcase .showborder') * showcaseClosedSize) - showcaseOffset + 'px' }, { queue: false });
		});
	});
});

function scheduleTimeThings() {
	// Schedule dótið :D
	
	if (!(typeof(times) === "undefined"))
	{
		var selectBox = '<select class="hours" name="hour">';
		var selected = true;

		for (i = 0; i < 24; i++)
		{
			if(times[i][0] || times[i][15] || times[i][30] || times[i][45])
			{
				selectBox += '<option value="' + i + '"' + (selected ? ' selected' : '') + '>' + ((i + '').length == 1 ? '0' + i : i) + '</option>';
				selected = false;
			}
		}

		selectBox += '</select><select class="minute" name="minute"></select><br />';

		$('#related .suggestion .btn.add').before(selectBox);
		$('#related .suggestion .hours').change(function () {
			var minuteOptions = '';

			for (j = 0; j < 4; j++)
			{
				if (times[eval($(this).val())][j * 15])
				{
					minuteOptions += '<option value="' + (j * 15) + '">' + (j == 0 ? '00' : j*15) + '</option>';
				}
			}
			$(this).parent().find('.minute').empty().append(minuteOptions);
		});
	}
}
